home *** CD-ROM | disk | FTP | other *** search
/ Aminet 28 / Aminet 28 (1998)(GTI - Schatztruhe)[!][Dec 1998].iso / Aminet / dev / e / kyz_obj.lha / simplebitfield.e < prev    next >
Text File  |  1998-09-09  |  4KB  |  167 lines

  1. OPT MODULE
  2.  
  3. ENUM B_SET, B_CLR, B_TST, B_MAX
  4.  
  5. /****** simplebitfield.m/--overview-- **************************************
  6. *
  7. *   PURPOSE
  8. *    To provide a simple bitfield.
  9. *
  10. *   OVERVIEW
  11. *    Implements  the  same  concepts  as  bitfield.m,  but a great many
  12. *    services  are  removed  or simplified, for applications where only
  13. *    the simple get/set/clear functionality of a bitfield is needed.
  14. *
  15. *   SEE ALSO
  16. *    bitfield.m
  17. *
  18. ****************************************************************************
  19. *
  20. *
  21. */
  22.  
  23. EXPORT OBJECT simplebitfield PRIVATE
  24.   data    -> PTR to data storing the bitfield
  25.   size    -> size (in bytes) of this data
  26. ENDOBJECT
  27.  
  28.  
  29. /****** simplebitfield.m/new *******************************************
  30. *
  31. *   NAME
  32. *    simplebitfield.new() -- Constructor.
  33. *
  34. *   SYNOPSIS
  35. *    new(max)
  36. *
  37. *   FUNCTION
  38. *    Initialises  an  instance  of the bitfield class. Raises exception
  39. *    "MEM"  if it cannot allocate enough memory for the required number
  40. *    of bits. All bits are initially cleared.
  41. *
  42. *   INPUT
  43. *    max   - the  maximum integer value that will be represented in the
  44. *            field. Must be positive. The minimum will be 0.
  45. *
  46. *   SEE ALSO
  47. *    end(), clearfield()
  48. *
  49. ****************************************************************************
  50. *
  51. *
  52. */
  53.  
  54. EXPORT PROC new(max) OF simplebitfield 
  55.   self.size := Shr(max + 7, 3)
  56.   self.data := NewR(self.size)
  57. ENDPROC
  58.  
  59. /****** simplebitfield.m/end *******************************************
  60. *
  61. *   NAME
  62. *    simplebitfield.end() -- Destructor.
  63. *
  64. *   SYNOPSIS
  65. *    end()
  66. *
  67. *   FUNCTION
  68. *    Frees resources used by an instance of the bitfield class.
  69. *
  70. *   SEE ALSO
  71. *    new()
  72. *
  73. ****************************************************************************
  74. *
  75. *
  76. */
  77.  
  78. EXPORT PROC end() OF simplebitfield IS Dispose(self.data)
  79.  
  80.  
  81. /****** simplebitfield.m/clearfield *******************************************
  82. *
  83. *   NAME
  84. *    simplebitfield.clearfield() -- clear all bits.
  85. *
  86. *   SYNOPSIS
  87. *    clear()
  88. *
  89. *   FUNCTION
  90. *    Clears all bits in the bitfield to boolean FALSE.
  91. *
  92. ****************************************************************************
  93. *
  94. *
  95. */
  96.  
  97. EXPORT PROC clearfield() OF simplebitfield
  98.   DEF mem, end
  99.   mem := self.data
  100.   end := mem + self.size
  101.   WHILE mem < end DO mem[]++ := 0
  102. ENDPROC
  103.  
  104.  
  105. /****** simplebitfield.m/bit_operations *******************************************
  106. *
  107. *   NAME
  108. *    simplebitfield.set() -- set an individual bit.
  109. *    simplebitfield.clear() -- clear an individual bit.
  110. *    simplebitfield.test() -- test an individual bit.
  111. *
  112. *   SYNOPSIS
  113. *    state := set(bit)
  114. *    state := clear(bit)
  115. *    state := test(bit)
  116. *
  117. *   FUNCTION
  118. *    Will  test,  then perform an operation on an individual bit in the
  119. *    bitfield:
  120. *
  121. *    set()    will set the bit to boolean TRUE.
  122. *    clear()  will clear the bit to boolean FALSE.
  123. *    test()   will perform no altering operation on the bit.
  124. *
  125. *    The  bit  specified  must  not lie outwith the range stored by the
  126. *    bitfield, otherwise innocent data _will_ be corrupted.
  127. *
  128. *   INPUTS
  129. *    bit   - the bit to perform an operation on.
  130. *
  131. *   RESULT
  132. *    state - the  previous  state  of  the bit before the operation was
  133. *            performed on it, either TRUE or FALSE.
  134. *
  135. ****************************************************************************
  136. *
  137. *
  138. */
  139.  
  140. EXPORT PROC set(bit)   OF simplebitfield IS bitop(B_SET, self, bit)
  141. EXPORT PROC clear(bit) OF simplebitfield IS bitop(B_CLR, self, bit)
  142. EXPORT PROC test(bit)  OF simplebitfield IS bitop(B_TST, self, bit)
  143.  
  144. PROC bitop(op, self:PTR TO simplebitfield, bit)
  145.   DEF offset:REG, realbit:REG, data
  146.  
  147.   data := self.data
  148.  
  149.   offset := Shr(bit AND -32, 3)
  150.   realbit := bit AND 31
  151.  
  152.     MOVE.L    data, A0
  153.     ADDA.L    offset, A0
  154.     MOVEQ    #0,D0
  155.  
  156.   SELECT B_MAX OF op
  157.   CASE B_SET;    BSET.L realbit,(A0)
  158.   CASE B_CLR;    BCLR.L realbit,(A0)
  159.   CASE B_TST;    BTST.L realbit,(A0)
  160.   ENDSELECT
  161.  
  162.     BEQ.S notset
  163.  
  164.   RETURN TRUE
  165. notset:
  166. ENDPROC FALSE
  167.